<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 集合是由一组无序且唯一（即不能重复）的项组成的

        class SetList {
            constructor() {
                this.items = {};
            }
            // 判断是否有某元素
            has(value) {
                // return value in this.items;
                // 更好的实现方法
                return this.items.hasOwnProperty(value);
            }

            // 添加
            add(value) {
                if (!this.has(value)) {
                    this.items[value] = value;
                    return true;
                }
                return false;
            }

            // remove
            remove(value) {
                if (this.has(value)) {
                    let temp = this.items[value];
                    Reflect.deleteProperty(this.items, value);
                    return temp;
                }
                return null;
            }

            // clear
            clear() {
                this.items = {};
            }

            // size
            size() {
                return Reflect.ownKeys(this.items).length;
            }

            // values
            values() {
                return Reflect.ownKeys(this.items);
            }

            // 并集
            union(otherSet) {
                let unionSet = new SetList();

                let values = this.values();
                for (let i = 0; i < values.length; i++) {
                    unionSet.add(values[i]);
                }

                values = otherSet.values();
                for (let i = 0; i < values.length; i++) {
                    unionSet.add(values[i]);
                }

                return unionSet;
            }

            // 交集
            intersection(otherSet) {
                let unionSet = new SetList();

                let values = this.values();
                for (let i = 0; i < values.length; i++) {
                    if (otherSet.has(values[i])) {
                        unionSet.add(values[i]);
                    }
                }

                return unionSet;
            }

            // 差集
            difference(otherSet) {
                let unionSet = new SetList();

                let values = this.values();
                for (let i = 0; i < values.length; i++) {
                    if (!otherSet.has(values[i])) {
                        unionSet.add(values[i]);
                    }
                }

                return unionSet;
            }

            // 子集(是否为otherSet的子集)
            subset(otherSet) {
                if (otherSet.size() < this.size()) {
                    return false;
                } else {
                    let values = this.values();
                    for (let i = 0; i < values.length; i++) {
                        if (!otherSet.has(values[i])) {
                           return false;
                        }
                    }
                    return true;
                }
            }
        }

        let set = new SetList();
        let set2 = new SetList();
        let set3 = new SetList();
        set.add(1);
        set.add(2);
        set2.add(1);
        set2.add(3);
        set2.add(4);
        set3.add(1);
        set3.add(3);
        console.log(set.union(set2));
        console.log(set.intersection(set2));
        console.log(set.difference(set2));
        console.log(set3.subset(set2));
    </script>
</body>

</html>